home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-06 | 1.6 KB | 59 lines | [TEXT/GEOL] |
- Item 2808332 19-Feb-91 19:58PST
-
- From: SATORI Satori SW, Hugh Rogovy,PRT
-
- To: MACAPP.TECH$ MacApp Technical
-
- ------------------------------------------------------------------------------
-
- Sub: Re: Write/Read Problem
-
- Ernie,
-
- MPW Pascal always allocates an even number of bytes for STRING types.
-
- So in your case, the size of each element is going to be:
- kMaxDataLength=10;
- Size of STRING[kMaxDataLength] =
- (10 character bytes)+(1 length byte)+[1 pad byte]= 12 allocated bytes
-
- In your code, you are calculating the size of a STRING[kMaxDataLength] as 11
- bytes, causing you to write a partial data structure to disk.
-
- You got lucky with 7 elements, because your 7th string must have been <5
- characters long.
-
- For odd length strings (ie. STRING[5]), you'll never have this problem because
- string length + length byte adds up to an even value.
-
- The better way to write your code is:
-
-
- CONST
- kMaxDataLength=10;
- kMaxPoints=400;
- TYPE
- dataElementType = String[kMaxDataLength];
- VAR
- aDataArray: ARRAY[1..kMaxPoints] of dataElementType;
-
- WRITING:
- Count:=SIZEOF(dataElementType)*fNumberTimeUnits;
- FailOSErr(FSWrite(aRefNum,Count,@aDataArray));
-
- READING:
- Count:=SIZEOF(dataElementType)*fNumberTimeUnits;
- FailOSErr(FSRead(aRefNum,Count,@aDataArray));
-
- You need to define your string as a TYPE because the Pascal compiler can't
- handle SIZEOF(STRING[kMaxDataLength]). It's always a good idea to use SIZEOF,
- that way you don't need to make assumptions about how data is allocated by the
- compiler.
-
-
- Hope this helps,
-
- Chris Le Croy
- Satori Software
-
-